using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint.Workflow;

namespace Custom.Workflow.Activities
{
	public partial class InfoPathBaseActivity: Activity
	{
        protected  IPAccessHelper _ipHelper;

        protected bool AutoSaveForm = false;
        protected bool AutoLoadForm = true;

        #region  Base bound properties

        public static DependencyProperty __ContextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(InfoPathBaseActivity));

        [Description("Context")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get
            {
                return ((WorkflowContext)(base.GetValue(ResetListItemPermissionInheritance.__ContextProperty)));
            }
            set
            {
                base.SetValue(ResetListItemPermissionInheritance.__ContextProperty, value);
            }
        }
        #endregion

		public InfoPathBaseActivity()
		{
           
			InitializeComponent();
		}

       public InfoPathBaseActivity(string name):base(name)
		{
           
			InitializeComponent();
		}

        
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            _ipHelper = new IPAccessHelper(__Context);

            if (AutoLoadForm)
            {
                try
                {
                    _ipHelper.LoadForm();
                }
                catch (Exception e)
                {
                    Exception we = Common.WrapWithFriedlyException(e, "Error loading form data file. Likely causes: this file is not a valid InfoPath Form!");

                    Common.LogExceptionToWorkflowHistory(we, executionContext, this.WorkflowInstanceId);

                    throw we;

                }
            }

           ExecuteIPActivity(executionContext);

            if (AutoSaveForm)
            {
                try
                {
                    _ipHelper.SaveForm();
                }
                catch (Exception e)
                {
                    Exception we = Common.WrapWithFriedlyException(e, "Error saving form");

                    Common.LogExceptionToWorkflowHistory(we, executionContext, this.WorkflowInstanceId);

                    throw we;

                }
            }

           _ipHelper.Dispose();

            return ActivityExecutionStatus.Closed;

        }

        protected  virtual ActivityExecutionStatus ExecuteIPActivity(ActivityExecutionContext executionContext)
        {
            return ActivityExecutionStatus.Closed;
        }
	}
}
